﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.foreman
{
    public partial class foreman_apply_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected string startDate;
        protected string endDate;
        protected int type = 0;//预约类型
        protected int src = 0;//预约来源
        protected int status = 0;//状态
        protected int foreman_id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            ChkAdminLevel("foreman_apply_list", DTEnums.ActionEnum.View.ToString()); //检查权限
            this.startDate = DTRequest.GetQueryString("startDate");
            this.endDate = DTRequest.GetQueryString("endDate");
            this.type = DTRequest.GetQueryInt("type");
            this.src = DTRequest.GetQueryInt("src");
            this.status = DTRequest.GetQueryInt("status");
            this.foreman_id = DTRequest.GetQueryInt("foreman_id");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                TreeBind(); //绑定类别
                RptBind("id>0" + CombSqlTxt(), "id desc");
            }
        }
        #region 绑定类别=================================
        private void TreeBind()
        {
            //所属工长
            BLL.foreman_user bllForeman = new BLL.foreman_user();
            DataTable dtForeman = bllForeman.GetList(0, "status=0", "nick_name asc").Tables[0];

            this.ddlForemanId.Items.Clear();
            this.ddlForemanId.DataSource = dtForeman;
            this.ddlForemanId.DataTextField = "nick_name";
            this.ddlForemanId.DataValueField = "id";
            this.ddlForemanId.DataBind();
            this.ddlForemanId.Items.Insert(0, new ListItem("所有工长", ""));
        }
        #endregion

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            if (this.type > 0)
            {
                this.ddlApplyType.SelectedValue = this.type.ToString();
            }
            if (this.src > 0)
            {
                this.ddlApplySrc.SelectedValue = this.src.ToString();
            }
            if (this.status > 0)
            {
                this.ddlApplyStatus.SelectedValue = this.status.ToString();
            }
            this.ddlForemanId.SelectedValue = this.foreman_id.ToString();

            this.txtEndDate.Text = this.endDate;
            this.txtStartDate.Text = this.startDate;

            BLL.foreman_apply bll = new BLL.foreman_apply();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("foreman_apply_list.aspx", "startDate={0}&endDate={1}&type={2}&src={3}&status={4}&foreman_id={5}&page={6}",
               startDate, this.endDate, this.type.ToString(), this.src.ToString(), this.status.ToString(), this.foreman_id.ToString(), "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt()
        {
            StringBuilder strTemp = new StringBuilder();

            if (!string.IsNullOrEmpty(startDate))
            {
                strTemp.Append(" and apply_addtime>='" + startDate + "'");
            }

            if (!string.IsNullOrEmpty(endDate))
            {
                strTemp.Append(" and apply_addtime<='" + endDate + "'");
            }

            if (type > 0)
                strTemp.Append(" and apply_type=" + type);

            if (src > 0)
                strTemp.Append(" and apply_src=" + src);

            if (status > 0)
                strTemp.Append(" and apply_status=" + status);

            if (foreman_id > 0)
                strTemp.Append(" and foreman_id=" + foreman_id);

            return strTemp.ToString();
        }
        #endregion

        #region 返回用户每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("foreman_apply_list_page_size", "DTcmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        #region 返回状态=============================
        protected string GetStatus(int status)
        {
            string result = string.Empty;
            switch (status)
            {
                case 1:
                    result = "待确认";
                    break;
                case 2:
                    result = "已确认待安排";
                    break;
                case 3:
                    result = "已完成";
                    break;
                case 4:
                    result = "作废预约";
                    break;
            }
            return result;
        }

        protected string GetApplyType(int type)
        {
            string result = string.Empty;
            switch (type)
            {//预约类型1预约 2量房 3报价 4设计 5参观 6工长加盟  7设计师加盟 8建材
                case 1:
                    result = "预约";
                    break;
                case 2:
                    result = "量房";
                    break;
                case 3:
                    result = "报价";
                    break;
                case 4:
                    result = "设计";
                    break;
                case 5:
                    result = "参观";
                    break;
                case 6:
                    result = "工长加盟";
                    break;
                case 7:
                    result = "设计师加盟";
                    break;
                case 8:
                    result = "建材";
                    break;
            }
            return result;
        }

        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("foreman_apply_list.aspx", "startDate={0}&endDate={1}&type={2}&src={3}&status={4}&foreman_id={5}",
             this.txtStartDate.Text, this.txtEndDate.Text, this.ddlApplyType.SelectedValue, this.ddlApplySrc.SelectedValue, this.ddlApplyStatus.SelectedValue, this.ddlForemanId.SelectedValue)
         );
        }

        //筛选
        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("foreman_apply_list.aspx", "startDate={0}&endDate={1}&type={2}&src={3}&status={4}&foreman_id={5}",
             this.txtStartDate.Text, this.txtEndDate.Text, this.ddlApplyType.SelectedValue, this.ddlApplySrc.SelectedValue, this.ddlApplyStatus.SelectedValue, this.ddlForemanId.SelectedValue)
         );
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("foreman_apply_list_page_size", "DTcmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("foreman_apply_list.aspx", "startDate={0}&endDate={1}&type={2}&src={3}&status={4}&foreman_id={5}",
             this.txtStartDate.Text, this.txtEndDate.Text, this.ddlApplyType.SelectedValue, this.ddlApplySrc.SelectedValue, this.ddlApplyStatus.SelectedValue, this.ddlForemanId.SelectedValue)
         );
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("foreman_apply_list", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.foreman_apply bll = new BLL.foreman_apply();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除预约" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！",
             Utils.CombUrlTxt("foreman_apply_list.aspx", "startDate={0}&endDate={1}&type={2}&src={3}&status={4}&foreman_id={5}",
             this.txtStartDate.Text, this.txtEndDate.Text, this.ddlApplyType.SelectedValue, this.ddlApplySrc.SelectedValue, this.ddlApplyStatus.SelectedValue, this.ddlForemanId.SelectedValue)
         );
        }
    }
}